﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows.Input;

namespace TestShit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            _cars = new List<Car>();
            _firstCars = new List<Car>();
            _secondCars = new List<Car>();
            _thirdCars = new List<Car>();
        }

        UserControl1 uc;

  
        List<Car> _cars;
        List<Car> _firstCars;
        List<Car> _secondCars;
        List<Car> _thirdCars;

        private void Form1_Load(object sender, EventArgs e)
        {
            uc = new UserControl1();

            uc.XCanvas.MouseWheel += new MouseWheelEventHandler(canvas_MouseWheel);
         
            elementHost1.Child = uc;
        }


 
        void canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Car tempCar;
            tempCar = new Car();
            uc.XCanvas.Children.Add(tempCar);
            tempCar.AnimationCompleted += CarAnimationCompleted;
            tempCar.AnimationPath = uc.XPathGeometry;
            tempCar.AnimationDuration = 5;
            tempCar.OpenAnimation();
            tempCar.StartAnimation();
            _cars.Add(tempCar);
            _firstCars.Add(tempCar);
        }

        private void CheckCarsCollision(List<Car> cars)
        {
            int i;
            for (i = 1; i < cars.Count; i++)
            {
                if (cars[i].Collide(cars[i - 1]))
                {
                    cars[i].PauseAnimation();
                }
                else
                {
                    if (cars[i].IsAnimationPaused())
                    {
                        cars[i].ContinueAnimation();
                    }
                }
            }
        }

        private void tmCars_Tick(object sender, EventArgs e)
        {
            CheckCarsCollision(_firstCars);
            CheckCarsCollision(_secondCars);
            CheckCarsCollision(_thirdCars);
        }

        private void btnPauseFirstCar_Click(object sender, EventArgs e)
        {
            if ((_cars.Count > 0) && (_cars[0] != null))
            {
                _cars[0].PauseAnimation();
            }
        }

        private void btnStartFirstCar_Click(object sender, EventArgs e)
        {
            if ((_cars.Count > 0) && (_cars[0] != null))
            {
                _cars[0].ContinueAnimation();
            }
        }
        private void RemoveCarsBefore(List<Car> cars, Car car)
        {
            int i, j;
            for (i = 0; i < cars.Count; i++)
            {
                if (cars[i] == car)
                {
                    break;
                }
            }
            cars.RemoveRange(0, i);
        }
        private void CarAnimationCompleted(object sender, EventArgs e)
        {
            Car tempCar;
            tempCar = sender as Car;
            if (tempCar.AnimationPath == uc.XPathGeometry2)
            {                
                _thirdCars.Add(tempCar);
                RemoveCarsBefore(_secondCars, tempCar);
                tempCar.AnimationPath = uc.XPathGeometry4;
                tempCar.AnimationDuration = 5;
                tempCar.OpenAnimation();
                tempCar.StartAnimation();
            }
            else if (tempCar.AnimationPath == uc.XPathGeometry)
            {
                _secondCars.Add(tempCar);
                RemoveCarsBefore(_firstCars, tempCar);
                tempCar.AnimationPath = uc.XPathGeometry2;
                tempCar.AnimationDuration = 5;
                tempCar.OpenAnimation();
                tempCar.StartAnimation();
            }
            else
            {
                /*tempCar.AnimationPath = uc.XPathGeometry;
                tempCar.AnimationDuration = 5;
                tempCar.OpenAnimation();
                tempCar.StartAnimation();*/
            }
        }
    }
}
